﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Shared.Exceptions;

namespace DcsGauges.Shared.DataStructures
{
    class DcsA10Converter
    {
        //extracted from devices.lua
        private static readonly Dictionary<DataProperties, int>             propertyToDeviceMap = new Dictionary<DataProperties,int>()
        {
            { DataProperties.LeftCoreSpeedIndicatorUnits,  37},
            { DataProperties.LeftEngineTemperatureUntis, 37 }
        };

        //extracted from mainpanel_init.lua
        private static readonly Dictionary<DataProperties, int>             propertyToArgumentMap = new Dictionary<DataProperties,int>()
        {
            { DataProperties.LeftEngineTemperatureUntis, 71 },
        };

        //reversed from the above
        private static readonly Dictionary<int, DataProperties>             argumentDeviceToProperty = new Dictionary<int,DataProperties>()
        {
            { CombineIds(37, 71), DataProperties.LeftEngineTemperatureUntis },
        };

        private static int CombineIds(int deviceId, int argument)
        {
            return deviceId * 10000 + argument;
        }

        public static int DeviceId(DataProperties property)
        {
            int deviceId = -1;
            if (!propertyToDeviceMap.TryGetValue(property, out deviceId))
            {
                throw new DcsPropertyNotSupportedException(property);
            }
            
            return deviceId;
        }

        public static DataProperties Property(int deviceId, int argument)
        {
            int combinedId = CombineIds(deviceId, argument);
            DataProperties prop = DataProperties.Unknown;
            if (argumentDeviceToProperty.TryGetValue(combinedId, out prop) == false)
            {
                throw new DcsPropertyResolveException(deviceId, argument);
            }

            return prop;
        }
    }
}
